<?php
class delaypembeliancontroller extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('mastersupplier_model');
		$this->load->model('masterbarangdetail_model');
		$this->load->model('delaypembelian_model');
		$this->load->model('transaksibankpembelian_model');
		if(($this->session->userdata('fullname') == '') || ($this->session->userdata('fullname') == NULL)){
			redirect('logincontroller');
		}
	}
	
	function index(){	
		
		if (isset($_POST['btnview'])) {
            $this->delaypembelian_model->setNoPo($_POST['pokode']);
			$dataprofile = array(
				'page_title' => 'Purchasing > Delay Pembelian Barang',
				'name' => $this->session->userdata('fullname'),					
				'mstrpo' => $this->delaypembelian_model->getkodepo(),
				'transdelay' => $this->delaypembelian_model->getponolike(),
				'message' =>''		
			);
        } 
		else 
		{
			$config['uri_segment'] = 3;
			$config['base_url'] = site_url('delaypembeliancontroller/index');
			$config['total_rows'] = $this->delaypembelian_model->countGetList();
			
			$this->pagination_editable->initialize($config);			
		
			$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
			
			$dataprofile = array(
				'page_title' => 'Purchasing > Pembelian Barang Tertunda',
				'name' => $this->session->userdata('fullname'),				
				'mstrpo' => $this->delaypembelian_model->getkodepo(),
				'transdelay' => $this->delaypembelian_model->getList(),
				'message' =>''		
			);
		}
		$content = array(
			'content' => 'purchasing/delay_pembelian/inputdelaypembelian'
		);
		$this->template->load('templates/default',$content,$dataprofile);
	}
	
	function approvedelaypembelian() 
	{		
		if (isset($_POST['btnapprove'])) 
		{			
			$this->delaypembelian_model->setNoPo($_POST['txtNoPo'][0]);
			if (isset($_POST['keterangan']) && $_POST['keterangan'] !="")
			{
				//var_dump($_POST);exit;
				$jumdata = count($this->input->post('chkitem'));
				$i = 0;
				$ttlqty="";
				$ttlamt="";
				$tglfaktur =date('Y-m-d', strtotime('now'));
				$tanggal=date('Y-m-d H:i:s', strtotime('now'));
				
				$kodenofak = $this->transaksibankpembelian_model->getkodenofak();	
											
				foreach($this->input->post('chkitem') as $iddelay)
				{
					$kodedetailid = $this->transaksibankpembelian_model->getdetailid();
					
					$kddtlbarang = $_POST['chkitem'][$i];
					$kdesupp	 = $_POST['kdesupplier'][$i];
					$kdpono		 = $_POST['txtNoPo'][$i];
					$quanttity	 = $_POST['txtQuantity'][$i];
					$disct		 = $_POST['txtDiscount'][$i];
					$amount		 = $_POST['txtAmount'][$i];
					$kdiddetail	 = $_POST['kdeiddetail'][$i];
					$ttlqty		 = $ttlqty + $quanttity;
					$ttlamt		 = $ttlamt + $amount;
					
					$this->delaypembelian_model->setIDdetail($kdiddetail);
					$this->delaypembelian_model->approvedelaypembeliandetial();
					
					$this->transaksibankpembelian_model->setNoFaktur($kodenofak);
					$this->transaksibankpembelian_model->setIDdetail($kodedetailid);
					$this->transaksibankpembelian_model->setNoFaktur($kodenofak);
					$this->transaksibankpembelian_model->setNoPo($kdpono);
					$this->transaksibankpembelian_model->setKodeDetailBarang($kddtlbarang);
					$this->transaksibankpembelian_model->setQuantity($quanttity);
					$this->transaksibankpembelian_model->setDiscount($disct);
					$this->transaksibankpembelian_model->setAmount($amount);
					
					$this->transaksibankpembelian_model->insertdetail();					
					$i++;
				}
				
				$Qtyall = $this->delaypembelian_model->getTotalqty();

				if($Qtyall == $ttlqty)
				{	
					$this->delaypembelian_model->deletedelaypembelian();
				}
				else
				{
					$ttlquant = $Qtyall-$ttlqty;
					$this->delaypembelian_model->setTotalQuantity($ttlquant);
					$this->delaypembelian_model->approvedelaypembelian();
				}
				
				$this->transaksibankpembelian_model->setNoFaktur($kodenofak);
				$this->transaksibankpembelian_model->setNoPo($kdpono);
				$this->transaksibankpembelian_model->setDateFaktur($tglfaktur);
				$this->transaksibankpembelian_model->setTotalQuantity($ttlqty);
				$this->transaksibankpembelian_model->setTotalAmount($ttlamt);
				$this->transaksibankpembelian_model->setKeterangan($_POST['keterangan']);
				$this->transaksibankpembelian_model->setCreatedBy($this->session->userdata('userid'));
				$this->transaksibankpembelian_model->setCreatedOn($tanggal);
				
				$this->transaksibankpembelian_model->insert();
				
				$dataprofile = array(
					'page_title' => 'Purchasing > Pembelian Barang Tertunda',
					'name' => $this->session->userdata('fullname'),
					'kodedelayno'=> $this->transaksibankpembelian_model->ambilnofaktur(),
					'kodesuppdelay'=> $this->transaksibankpembelian_model->ambilsupplier(),
					'transdelay' => $this->transaksibankpembelian_model->getListfakturdelay()	
				);		
				$content = array(
					'content' => 'purchasing/delay_pembelian/formdelaypembelian'
				);
				$this->template->load('templates/default',$content,$dataprofile);
				
				//redirect('delaypembeliancontroller');
			}
			else
			{
				$dataprofile = array(
					'page_title' => 'Purchasing > Pembelian Barang Tertunda',
            		'name' => $this->session->userdata('fullname'),
					'mstrpo' => $this->delaypembelian_model->getkodepo(),
					'transdelay' => $this->delaypembelian_model->getList(),
					'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Keterangan Belum di isi </div>'
                );
                $content = array(
                    'content' => 'purchasing/delay_pembelian/inputdelaypembelian'
                );
                $this->template->load('templates/default', $content, $dataprofile);
			}
		}
		else
		{
			$dataprofile = array(
				'page_title' => 'Purchasing > Pembelian Barang Tertunda',
				'name' => $this->session->userdata('fullname'),
				'mstrpo' => $this->delaypembelian_model->getkodepo(),
				'transdelay' => $this->delaypembelian_model->getList(),
				'message' =>''		
			);		
			$content = array(
				'content' => 'purchasing/delay_pembelian/inputdelaypembelian'
			);
			$this->template->load('templates/default',$content,$dataprofile);
		}		
    }
}